<?php
/**
 * Created by PhpStorm.
 * User: dong
 * Date: 2018/6/2
 * Time: 15:58
 */
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\BankTraits\HYUNBankMap;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;

class HYUNPayment extends RechargeBaseAbstract implements WAPable
{
    use HYUNBankMap;

    protected $gw_pay = 'http://api.taobupa.com/Bank';
    protected $gw_query = 'http://api.taobupa.com/search.ashx';

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)) {
            if ($data['opstate'] == 0) return true;
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $signStr = "";
        $signStr .= "orderid=" . $data['orderid'] . '&';
        $signStr .= "opstate=" . $data['opstate'] . '&';
        $signStr .= "ovalue=" . $data['ovalue'];
        $signStr .= $this->getMchKey();
        if (strcasecmp($data['sign'], md5($signStr)) == 0) return true;
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['orderid'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['ovalue'];
    }

    public function getThirdNoFromCallback(array $data)
    {
        return $data['sysorderid'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::HYUN;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::ALI => '992',
            PaymentMap::WX => '1004',
            PaymentMap::QQ => '1008',
            PaymentMap::QQ_WAP => '1008',
            PaymentMap::WX_WAP => '1007',
            PaymentMap::ALI_WAP => '1006',
            PaymentMap::BANK_WAP => '1005',
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
        $this->setParameter('orderid', $rechargeOrder->plat_no);
        $this->setParameter('parter', $this->getMchId());
        $this->setParameter('sign', $this->querySign());
        $res = curlHttp($this->getGwQuery(), $this->getParameters());
        if ($res['http_code'] == 200) {
            parse_str($res['body'], $output);
            if (array_key_exists('opstate', $output) && $output['opstate'] == 0) return true;
            $errormap = [
                '3'=>"请求参数无效",
                '2'=>"签名错误",
                '1'=>"商户订单号无效",
                '0'=>"支付成功",
            ];
            $msg = array_key_exists($output['opstate'], $errormap) ? $errormap[$output['opstate']] : '用户还未完成支付或者支付失败';
            return '支付失败:' . $msg;
        }
        return $res['body'];
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() . '?' . http_build_query($this->getParameters());
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        $this->initParameters($rechargeOrder);

        return $this->getGwPay() . '?' . http_build_query($this->getParameters());
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $signStr = "";
        $signStr .= "parter=" . $this->getParameter('parter') . '&';
        $signStr .= "type=" . $this->getParameter('type') . '&';
        $signStr .= "value=" . $this->getParameter('value') . '&';
        $signStr .= "orderid=" . $this->getParameter('orderid') . '&';
        $signStr .= "callbackurl=" . $this->getParameter('callbackurl');
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
        $signStr = "";
        $signStr .= "orderid=" . $this->getParameter('orderid') . '&';
        $signStr .= "parter=" . $this->getParameter('parter') ;
        $signStr .= $this->getMchKey();
        return md5($signStr);
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'opstate=0';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement initParameters() method.
        $mchData = $rechargeOrder->order_data;
        if ($mchData['recharge_type'] == PaymentMap::BANK) {
            $type = $this->getBank($mchData['bank_code']);
        } else {
            $type = $this->getPaymentMap($mchData['recharge_type']);
        }
        $this->setParameter('parter', $this->getMchId());
        $this->setParameter('type', $type);
        $this->setParameter('value', $rechargeOrder->order_amt);
        $this->setParameter('orderid', $rechargeOrder->plat_no);
        $this->setParameter('callbackurl', $this->getCallbackUrl());
        $this->setParameter('hrefbackurl', $this->getReturnUrl());
        $this->setParameter('payerIp', request()->getClientIp());
        $this->setParameter('sign', $this->paySign());
    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        return new HrefRedirectAction($this->getGwPay() . '?' . http_build_query($this->getParameters()));
    }
}